import Vue from 'vue'

import 'normalize.css/normalize.css' // A modern alternative to CSS resets
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import locale from 'element-ui/lib/locale/lang/zh-CN' // lang i18n
import '@/styles/index.scss' // global css
import App from './App'
import store from './store'
import router from './router'

import '@/icons' // icon
import '@/permission' // permission control
import CKEditor from '@ckeditor/ckeditor5-vue';
import commonUtil from '@/utils/commonUtil.js'
// 图片上传压缩
import * as imageConversion from 'image-conversion';

Vue.use(CKEditor);

Vue.prototype.$commonUtil = commonUtil.FUNCTIONS
//图片压缩
Vue.prototype.$elUploadBeforeUpload = function (file) {
    //图片大小超过0.5M,长度超过1000就压缩
    return new Promise((resolve, reject) => {
        var testmsg = file.name.substring(file.name.lastIndexOf(".") + 1);
        let arr = [
            "jpg",
            "jpeg",
            "png",
            "gif",
            "bmp",
            "JPG",
            "JPEG",
            "PNG",
            "GIF",
            "BMP"
        ];
        if (arr.indexOf(testmsg) === -1) {
            this.$message({
                type: "error",
                message: "上传图片格式有误"
            });
            return false; //return false就可以在格式大小不符后返回，不调用接口
        }
        const limit = file.size / 1024 / 1024 < 10;
        if (!limit) {
            this.$message({
                type: "error",
                message: "上传文件大小不能超过 10MB"
            });
            return false;
        }

        let _URL = window.URL || window.webkitURL
        let isLt2M = file.size / 1024 / 1024 > 0.2 // 判定图片大小是否小于0.2MB
        // 这里需要计算出图片的长宽
        let img = new Image()
        img.onload = function () {
            file.width = img.width // 获取到width放在了file属性上
            file.height = img.height // 获取到height放在了file属性上
            let valid = img.width > 600 // 图片宽度大于1000
            console.log('压缩前', file)
            // 这里我只判断了图片的宽度,compressAccurately有多个参数时传入对象
            if (valid || isLt2M) {
                // 大小在500k以下，宽度1000以下
                imageConversion.compressAccurately(file, {
                    size: 200,
                    width: 600
                }).then(res => {
                    console.log('压缩后', res)
                    resolve(res)
                })
            } else {
                resolve(file)
            }
        } // 需要把图片赋值
        img.src = _URL.createObjectURL(file)
    })
};

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online ! ! !
 */
if (process.env.NODE_ENV === 'production') {
    const { mockXHR } = require('../mock')
    mockXHR()
}

// set ElementUI lang to EN
Vue.use(ElementUI, { locale })
// 如果想要中文版 element-ui，按如下方式声明
// Vue.use(ElementUI)

Vue.config.productionTip = false

new Vue({
    el: '#app',
    router,
    store,
    render: h => h(App)
})
